package com.avic.rong.search.fragment.searchResultFragments;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.avic.rong.BaseActivity;
import com.avic.rong.BaseFragment;
import com.avic.rong.R;
import com.avic.rong.bean.show_bean.EsInfoBean;
import com.avic.rong.fragment.showapi.EsInfoApi;
import com.avic.rong.goodsSort.bean.showRvBean.GoodsSortSearchAllBzBean;
import com.avic.rong.search.MessageEvent;
import com.avic.rong.search.activity.SearchResaultActivity;
import com.avic.rong.search.adapter.searchResultFragmentsAdapters.SearchArticleRvAdapter;
import com.avic.rong.search.api.secondViewApi.SearchHomeQuanBuApi;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class SearchArticleFragment extends BaseFragment implements OnRefreshListener, OnLoadMoreListener {

    @BindView(R.id.search_Article_Rv)
    RecyclerView searchArticleRv;
    Unbinder unbinder;
    @BindView(R.id.search_vp_article_tv)
    TextView searchVpArticleTv;
    @BindView(R.id.beijing_image)
    ImageView beijing_image;
    @BindView(R.id.search_fragment_article_SmartRefreshLayout)
    SmartRefreshLayout searchFragmentArticleSmartRefreshLayout;
    private View view;
    private List<GoodsSortSearchAllBzBean.DataBean> list = new ArrayList<>();
    private SearchArticleRvAdapter adapter;
    private String currentText = "GJB";
    private static int pageNo = 1;
    private ArrayList<EsInfoBean> esInfoBean;
    private ArrayList<EsInfoBean> esList = new ArrayList<>();
    private SearchHomeQuanBuApi searchAllApi;

    public SearchArticleFragment() {
        // Required empty public constructor
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        currentText = SearchResaultActivity.nowSearchStr;
        Log.e("cjx", "SearchQuanBuFragment--------currentText:" + currentText);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_search_article, container, false);
        unbinder = ButterKnife.bind(this, view);
        list.clear();
        esList.clear();
        initArticleData(currentText, pageNo);
        searchFragmentArticleSmartRefreshLayout.setOnRefreshListener(this);
        searchFragmentArticleSmartRefreshLayout.setOnLoadMoreListener(this);
        searchArticleRv.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new SearchArticleRvAdapter(getContext(), list, esList);
        searchArticleRv.setAdapter(adapter);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        return view;
    }

    public void initArticleData(String keyword, int pageNo) {
        searchAllApi = new SearchHomeQuanBuApi((BaseActivity) getActivity(), searchHomeQuanBuListener, "quanbu", 5, keyword, pageNo, 10);
        Log.e("cjx", "bzkeyWord:" + keyword);
        searchAllApi.setCache(false);
        HttpManager.getInstance().doHttpDeal(searchAllApi);

    }

    //获取标准数据监听
    HttpOnNextListener<BaseResultEntity> searchHomeQuanBuListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            if (!subjects.getCode().equals("200")) {
                Log.e("cjx", "biaozhun数据：" + subjects.getMsg());
                return;
            }


            ArrayList<GoodsSortSearchAllBzBean> searchHomeQuanBuList = (ArrayList<GoodsSortSearchAllBzBean>) subjects.getData();
            list.addAll(searchHomeQuanBuList.get(3).getData());
            adapter.notifyDataSetChanged();
            //标准
            if (list.size() > 0) {
                searchArticleRv.setVisibility(View.VISIBLE);
                searchVpArticleTv.setVisibility(View.GONE);
                beijing_image.setVisibility(View.GONE);
                String substring = "";
                String countId = "";
                for (int i = 0; i < list.size(); i++) {
                    int dataId = list.get(i).getDataId();
                    countId += dataId + ",";
                }
                substring = countId.substring(0, countId.length() - 1);
                EsInfoApi esInfoApi = new EsInfoApi((RxAppCompatActivity) getActivity(), EnsOnNextListener, substring);
                esInfoApi.setCache(false);
                HttpManager.getInstance().doHttpDeal(esInfoApi);

            } else {
                searchArticleRv.setVisibility(View.GONE);
                searchVpArticleTv.setVisibility(View.VISIBLE);
                beijing_image.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public void onCacheNext(String cache) {
        }

        @Override
        public void onError(final Throwable e) {

        }
    };
    //搜索补全接口
    HttpOnNextListener<BaseResultEntity> EnsOnNextListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            String code = baseResultEntity.getCode();
            if (code.equals("200")) {
                esInfoBean = (ArrayList<EsInfoBean>) baseResultEntity.getData();
                esList.addAll(esInfoBean);
                adapter.notifyDataSetChanged();
            } else {
                Toast.makeText(getActivity(), "没有数据", Toast.LENGTH_SHORT).show();
            }
        }
    };

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
        unbinder.unbind();
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void addListener() {

    }

    @Override
    protected int getContentViewId() {
        return 0;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void MesssageEventBus(MessageEvent event) {
        list.clear();
        adapter.notifyDataSetChanged();
        HttpManager.getInstance().doHttpDeal(searchAllApi);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        list.clear();
        pageNo = 1;
        initArticleData(currentText, pageNo);
        searchFragmentArticleSmartRefreshLayout.finishRefresh(1000);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        ++pageNo;
        initArticleData(currentText, pageNo);
        searchFragmentArticleSmartRefreshLayout.finishLoadMore(1000);
    }
}
